import iView from 'iview'
import i18n from '@/locale'

function handleErrorFun (error) {
  const $t = function (key) {
    return i18n.t(key)
  }
  console.error(error)
  iView.LoadingBar.error()
  iView.Message.error(error.code && $t(error.code) && $t(error.code) !== error.code ? $t(error.code) : (error.msg || error))
}

export default {
  state: {
    note: ''
  },
  mutations: {
    handleError (state, error) {
      handleErrorFun(error)
    },
    startLoading (state) {
      iView.LoadingBar.start()
    },
    updateLoading (state, percent) {
      iView.LoadingBar.update(percent)
    },
    finishLoading (state) {
      iView.LoadingBar.finish()
    },
    showMessageSuccess (state, message) {
      iView.Message.success(message)
    },
    showMessageInfo (state, message) {
      iView.Message.info(message)
    },
    getNote (state) {
      let note = window.localStorage.getItem('note')
      state.note = note
    },
    setNote (state, note) {
      window.localStorage.setItem('note', note)
      state.note = note
    }
  }
}
